
import 'package:baselib/utils/keyboard_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/md5_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/model/user_entity.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';

class LoginEmailViewModel extends BaseViewModel<ModuleCommService>{

  String account = '';
  String password = '';
  //是否同意用户协议
  RxBool isAgreementSelected=true.obs;
  ModuleCommService moduleCommService;

  @override
  ModuleCommService createService() =>moduleCommService = ModuleCommService();

  @override
  void onInit() {
    //初始化tab
    // account = UserParamsModel.instance.account;
    // password = UserParamsModel.instance.password;
    super.onInit();
  }

  inputChanged(String value, int index) {
    switch(index){
      case 0:
        account=value?.trim();
        break;
      case 1:
        password=value?.trim();
        break;
    }
  }

  login() async{
    if(!(isValidate()))return;
    Map<String,dynamic> upData={
      'email':account,
      'password':Md5Util.generateMd5(
          Md5Util.generateMd5(password + ApiConfig.wyyx_app_id)),
    };
    LoadUtil.showLoad();
    BaseEntity baseEntity = await moduleCommService.login(parmsMap: upData);
    KeyBoardUtil.downKeyBoard();
    LoadUtil.hideLoad();
    if (baseEntity?.code == ApiConfig.SUCCESS_CODE) {
      LogUtil.d('baseEntity=${baseEntity?.toJson()}');
      UserEntity userEntity = baseEntity?.result;
      if(userEntity!=null){
        var valid = userEntity.valid??(-1);
        // UserParamsModel.instance
        //     .setToken(userEntity.token).setAccount(account).setPassword(password)
        //   .setUserId('${userEntity.userId}').setImToken(userEntity.imToken)
        //   .setYxAccid(userEntity.yxAccid).setUserEntity(userEntity);
        if(valid==2){
          //完善信息页
          NavigatorUtil.pushByName(RouteConfig.registerInfoPage);
        }else{
          //直接去主页
          NavigatorUtil.offAllByName(RouteConfig.homePage);
        }
      }
    } else {
      ToastUtil.toast(baseEntity?.message);
    }
  }

  selectAgreement() {
    isAgreementSelected.value=!isAgreementSelected.value;
  }

  isValidate() {
    if(account.isEmpty){
      ToastUtil.toast(StringUtil.instance.Please_enter_your_account_number);
      return false;
    }
    if(password.isEmpty){
      ToastUtil.toast(StringUtil.instance.Please_enter_your_password);
      return false;
    }
    return true;
  }

  toTermsOfService() {}

  toPrivacyPolicy() {}

  toRegister() {
    NavigatorUtil.pushByName(RouteConfig.registerPage);
  }

  toForgetPasswordPage() {
    NavigatorUtil.pushByName(RouteConfig.forgetPasswordPage,resultFunc: (value){
      if((value!=null)&&(value is Map)){
        account = value['email'];
        update();
      }
    });
  }

}
